package code

import (
	"context"
	"fmt"
	"goStudy/grpc/helloword/helloword"
	"io"
)

type HelloServiceServer struct{}

func (this *HelloServiceServer) Hello(ctx context.Context, req *helloword.String) (*helloword.String, error) {
	fmt.Println("Hello调用,服务端收到消息:",req.Value)

	ack:=&helloword.String{
		Value:                "server:"+req.Value,
	}
	return ack,nil
}

func (this *HelloServiceServer) Channel(stream helloword.HelloService_ChannelServer) error {
	for {
		args,err:=stream.Recv()
		if err!= nil {
			if err==io.EOF {
				fmt.Println("客户端已断开连接")
				return nil
			}

			return err
		}

		ack:=&helloword.String{Value:"from server:"+args.GetValue()}
		err=stream.Send(ack)
		if err!= nil {
			return err
		}
	}
}
